//
//  LMProfilePostViewController.m
//  舍品
//
//  Created by 楼 眉 on 16/3/23.
//  Copyright © 2016年 楼 眉. All rights reserved.
//
#pragma mark - 帖子内容控制器
#import "LMProfilePostViewController.h"
#import "LMPost.h"
#import "LMPostFrame.h"
#import "LMReply.h"
#import "LMCommentController.h"
#import "LMReplyView.h"
#import "LMWarningLabel.h"
#include "LMMoreCommentController.h"

#import <YYWebImage.h>
#import <SVProgressHUD.h>
#import <MJRefresh.h>
#import <ReactiveCocoa.h>



static          int                  const       BottomViewHeight = 49;
static          int                  const       StatusBarAndNavigationBarHeight = 20 + 44;
static     CGFloat            const       writerViewHeight = 45;
static     CGFloat            const       nameLabelFont = 15;
static     CGFloat            const       titleLabelFont = 18;
static     CGFloat            const       timeLabelFont = 13;
static     CGFloat            const       contentLabelFont = 20;
static     CGFloat            const       LmSpace = 10;
static       double             const       bottomLineRGBValue = 230;
static    NSString    *const    KUserInformationChange   =   @"userInformationChange";


@interface LMProfilePostViewController ()

/**
 *  滚动大背景
 */
@property (nonatomic,strong)  UIScrollView *bgView;
/**
 *  帖子背景
 */
@property (nonatomic,strong)  UIImageView *postBgView;
/**
 *  评论背景
 */
@property (nonatomic,strong)  UIImageView *commentBgView;
/**
 *  留言背景
 */
@property (nonatomic,strong)  UIImageView *replyBgView;
/**
 *  所有的评论数据
 */
@property (nonatomic,strong)  NSArray *reviews;
/**
 *  背景偏移量
 */
@property (nonatomic,assign) CGPoint offset;
/**
 *  更多评论
 */
@property (nonatomic,strong)  UIButton *moreCommentBtn;
@end

@implementation LMProfilePostViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    [self firstReceiveData];
    __weak typeof(self) weakSelf = self;
    _bgView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf receiveData];
    }];
}
#pragma mark - 刷新评论
- (void)refreshReviews
{
    self.offset = _bgView.contentOffset;
    AVRelation *relation = [self.post relationForKey:@"reviews"];
    AVQuery *queryForReview = [relation query];
    [queryForReview orderByDescending:@"createdAt"];
    __weak typeof(self) weakSelf = self;
    [queryForReview findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!objects.count == 0)
        {
            weakSelf.reviews = objects;
            _bgView.contentOffset = weakSelf.offset;
        }
        else
        {
            [_bgView.mj_header endRefreshing];
        }
    }];
}
#pragma mark - 第一次接收数据
- (void)firstReceiveData
{
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    if (manager.isReachable == NO) {
        [LMWarningLabel LM_createWarningLabelWithTitle:nil];
    }
    else
    {
    [SVProgressHUD showWithStatus:@"加载中..."];
    self.post = self.post;
    AVRelation *relation = [self.post relationForKey:@"reviews"];
    AVQuery *queryForReview = [relation query];
    [queryForReview orderByDescending:@"createdAt"];
        queryForReview.limit = 3;
    __weak typeof(self) weakSelf = self;
    [queryForReview findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        if (!objects.count == 0)
        {
            [SVProgressHUD dismiss];
            weakSelf.reviews = objects;
        }
        else
        {
            [SVProgressHUD dismiss];
        }
    }];
    }
}
#pragma mark -  接受数据
- (void)receiveData
{
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    if (manager.isReachable == NO) {
        [_bgView.mj_header endRefreshing];
        [LMWarningLabel LM_createWarningLabelWithTitle:nil];
    }else
    {
        self.post = self.post;
        AVRelation *relation = [self.post relationForKey:@"reviews"];
        AVQuery *queryForReview = [relation query];
        [queryForReview orderByDescending:@"createdAt"];
        __weak typeof(self) weakSelf = self;
        [queryForReview findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            if (objects.count)
            {
                weakSelf.reviews = objects;
            }
            else
            {
                [_bgView.mj_header endRefreshing];
            }
        }];
    }
}
#pragma mark - 从reviews数组中取出各个reply备用
- (void)setReviews:(NSArray *)reviews
{
    _reviews = reviews;
    [self setupNewReplyTitleView];
    for (int reviewIndex = 0; reviewIndex < reviews.count; reviewIndex ++) {
        LMReply *reply = reviews[reviewIndex];
        LMUser *user = reply.user;
        [self setupReviewUIWithLMReply:reply LMUser:user];
    }
}
#pragma mark - 初始化不需要网络数据的基础控件
- (void)setupNewReplyTitleView
{
    _commentBgView = [[UIImageView alloc]init];
    [self.bgView addSubview:_commentBgView];
    _commentBgView.backgroundColor = [UIColor whiteColor];
    _commentBgView.frame = CGRectMake(0, CGRectGetMaxY(self.postBgView.frame) + LmSpace, SCREEN_WIDTH, 0);
    
    UIImageView *commentTitleView = [[UIImageView alloc]init];
    commentTitleView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 33);
    [_commentBgView addSubview:commentTitleView];
    
    UIImageView *commentTitlePictureView = [[UIImageView alloc]init];
    commentTitlePictureView.image = [UIImage imageNamed:@"publish-review"];
    commentTitlePictureView.width = 33;
    commentTitlePictureView.height = 33;
    commentTitlePictureView.x = LmSpace;
    commentTitleView.y = LmSpace;
    [commentTitleView addSubview:commentTitlePictureView];
    
    UILabel *commentTitleLabel = [[UILabel alloc]init];
    commentTitleLabel.frame = CGRectMake(50, 0, 200, 33);
    commentTitleLabel.text = @"最新评论";
    commentTitleLabel.font = [UIFont systemFontOfSize:nameLabelFont];
    [commentTitleView addSubview:commentTitleLabel];
    
    UIView *commentTitleBottomLine = [[UIView alloc]init];
    commentTitleBottomLine.frame = CGRectMake(LmSpace, CGRectGetMaxY(commentTitlePictureView.frame) + LmSpace + LmSpace, SCREEN_WIDTH - 2 *LmSpace, 1);
    commentTitleBottomLine.backgroundColor = RGBCOLOR(bottomLineRGBValue, bottomLineRGBValue, bottomLineRGBValue);
    [_commentBgView addSubview:commentTitleBottomLine];
    
    _commentBgView.height = CGRectGetMaxY(commentTitleBottomLine.frame) + LmSpace;
    self.bgView.contentSize = CGSizeMake(0, CGRectGetMaxY(_commentBgView.frame));
    
    _replyBgView = [[UIImageView alloc]init];
    _replyBgView.userInteractionEnabled = YES;
    [self.bgView addSubview:_replyBgView];
    _replyBgView.frame = CGRectMake(0, CGRectGetMaxY(_commentBgView.frame), SCREEN_WIDTH, 0);
}

- (void)setupUI
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"帖子正文";
    
    UIButton *rightBtn = [[UIButton alloc]init];
    rightBtn.frame = CGRectMake(0, 0, 66*kUnit, 44*kUnit);
    [rightBtn setTitle:@"删除" forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [rightBtn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
    [rightBtn addTarget:self action:@selector(cancelPost) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightItem;
    //基础控件
    _bgView = [[UIScrollView alloc]init];
    _bgView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:_bgView];
    CGFloat bgViewX = 0;
    CGFloat bgViewY = StatusBarAndNavigationBarHeight;
    CGFloat bgViewW = SCREEN_WIDTH;
    CGFloat bgViewH = SCREEN_HEIGHT - BottomViewHeight - StatusBarAndNavigationBarHeight ;
    _bgView.frame = CGRectMake(bgViewX, bgViewY, bgViewW, bgViewH);
    _bgView.alwaysBounceVertical = YES;
    
    UIImageView *bottomView = [[UIImageView alloc]init];
    bottomView.userInteractionEnabled = YES;
    bottomView.backgroundColor = RGBCOLOR(240, 240, 240);
    [self.view addSubview:bottomView];
    CGFloat bottomViewH = BottomViewHeight;
    CGFloat bottomViewX = 0;
    CGFloat bottomViewY = CGRectGetMaxY(_bgView.frame) ;
    CGFloat bottomViewW = SCREEN_WIDTH;
    bottomView.frame = CGRectMake(bottomViewX, bottomViewY, bottomViewW, bottomViewH);
    
    UIButton *commentBtn = [[UIButton alloc]init];
    [bottomView addSubview:commentBtn];
    commentBtn.frame = CGRectMake(0, 0, bottomView.width / 2, bottomView.height);
    [commentBtn setTitle:@"忍不住了，我也要说两句" forState:UIControlStateNormal];
    commentBtn.titleLabel.font = [UIFont systemFontOfSize:11];
    [commentBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [commentBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateHighlighted];
    [commentBtn addTarget:self action:@selector(toCommentVc) forControlEvents:UIControlEventTouchUpInside];
    
    UIView *commentLine = [[UIView alloc]init];
    [bottomView addSubview:commentLine];
    commentLine.frame = CGRectMake(0, bottomViewH - 7, commentBtn.width, 1);
    commentLine.backgroundColor = [UIColor blackColor];
    
    UIButton *moreCommentBtn = [[UIButton alloc]init];
    [moreCommentBtn setImage:[UIImage imageNamed:@"mainCellComment"] forState:UIControlStateNormal];
    [moreCommentBtn setImage:[UIImage imageNamed:@"mainCellCommentClick"] forState:UIControlStateHighlighted];
    moreCommentBtn.frame = CGRectMake(SCREEN_WIDTH / 2, 0, SCREEN_WIDTH / 6, bottomView.height);
    [moreCommentBtn addTarget:self action:@selector(moreCommentBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:moreCommentBtn];
    
//    UIButton *goodBtn = [[UIButton alloc]init];
//    [goodBtn setImage:[UIImage imageNamed:@"mainCellComment"] forState:UIControlStateNormal];
//    [goodBtn setImage:[UIImage imageNamed:@"mainCellCommentClick"] forState:UIControlStateHighlighted];
//    goodBtn.frame = CGRectMake(bottomView.width - 33, 0, 33, bottomView.height);
//    [bottomView addSubview: goodBtn];
//    
//    UIButton *shareBtn = [[UIButton alloc]init];
//    [shareBtn setImage:[UIImage imageNamed:@"mainCellShare"] forState:UIControlStateNormal];
//    [shareBtn setImage:[UIImage imageNamed:@"mainCellShareClick"] forState:UIControlStateHighlighted];
//    shareBtn.frame = CGRectMake(CGRectGetMinX(goodBtn.frame) - 33, 0, 33, bottomView.height);
//    [bottomView addSubview: shareBtn];
//    
//    UIButton *messageBtn = [[UIButton alloc]init];
//    [messageBtn setImage:[UIImage imageNamed:@"mainCellDing"] forState:UIControlStateNormal];
//    [messageBtn setImage:[UIImage imageNamed:@"mainCellDingClick"] forState:UIControlStateHighlighted];
//    messageBtn.frame = CGRectMake(CGRectGetMinX(shareBtn.frame) - 33, 0, 33, bottomView.height);
//    [bottomView addSubview: messageBtn];
}

#pragma mark - 删除该帖子
- (void)cancelPost
{
    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"确定删除此贴？" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *notReal = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alertVc addAction:notReal];
    __weak typeof(self) weakSelf = self;
    UIAlertAction *real = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [SVProgressHUD showWithStatus:@"删除中..."];
        [weakSelf.post deleteEventuallyWithBlock:^(id object, NSError *error) {
            if (!error) {
                [SVProgressHUD dismiss];
                [LMWarningLabel LM_createWarningLabelWithTitle:@"删除成功"];
                [weakSelf.navigationController popViewControllerAnimated:YES];
                [[NSNotificationCenter defaultCenter] postNotificationName:KUserInformationChange object:nil];
            }
            else
            {
                [SVProgressHUD dismiss];
                [LMWarningLabel LM_createWarningLabelWithTitle:@"删除失败"];
            }
        }];
    }];
    [alertVc addAction:real];
    [self presentViewController:alertVc animated:YES completion:^{
        
    }];
}
#pragma mark - postSet方法 时候 赋值给各控件
- (void)setPost:(LMPost *)post
{
    _post = post;
    //作者栏
    UIImageView *writerView = [[UIImageView alloc]init];
    writerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, writerViewHeight);
    writerView.backgroundColor = [UIColor whiteColor];
    [self.bgView addSubview:writerView];
    
    UIImageView *iconImageView = [[UIImageView alloc]init];
    iconImageView.frame = CGRectMake(LmSpace, 5, writerView.height - 10, writerView.height - 10);
    LMUser *user = post.writer;
    [CALayer LM_RoundedImageWIthImageViewCALayer:iconImageView.layer Width:iconImageView.width];
    [user fetchInBackgroundWithBlock:^(AVObject *object, NSError *error) {
        AVFile *file = [object objectForKey:@"iconImage"];
        NSURL *url = [NSURL URLWithString:file.url];
        [iconImageView yy_setImageWithURL:url placeholder:[UIImage imageNamed:@"占位图.png"]];
        
        UILabel *nameLabel = [[UILabel alloc]init];
        nameLabel.textAlignment = NSTextAlignmentLeft;
        nameLabel.text = user.nickname;
        nameLabel.numberOfLines = 0;
        nameLabel.font = [UIFont systemFontOfSize:nameLabelFont];
        nameLabel.textColor = [UIColor grayColor];
        NSDictionary *nameLabelAttri = @{NSFontAttributeName:[UIFont systemFontOfSize:nameLabelFont]};
        CGSize  nameLabelSize = [user.nickname boundingRectWithSize:CGSizeMake(writerView.width - iconImageView.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:nameLabelAttri context:nil].size;
        nameLabel.frame = (CGRect){{0,0},nameLabelSize};
        nameLabel.center = writerView.center;
        nameLabel.x = CGRectGetMaxX(iconImageView.frame) + 5;
        [writerView addSubview:nameLabel];
    }];
    [writerView addSubview:iconImageView];
    
    UIView *writerBottomLine = [[UIView alloc]init];
    writerBottomLine.frame = CGRectMake(LmSpace,CGRectGetMaxY(iconImageView.frame) + LmSpace, writerView.width - 20, 1);
    writerBottomLine.backgroundColor = RGBCOLOR(bottomLineRGBValue, bottomLineRGBValue, bottomLineRGBValue);
    [writerView addSubview:writerBottomLine];
    
    writerView.height = CGRectGetMaxY(writerBottomLine.frame);
    
    //标题栏
    UIImageView *titleView = [[UIImageView alloc]init];
    titleView.frame = CGRectMake(0, CGRectGetMaxY(writerView.frame), SCREEN_WIDTH, 0);
    titleView.backgroundColor = [UIColor whiteColor];
    [self.bgView addSubview:titleView];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.textAlignment = NSTextAlignmentLeft;
    titleLabel.text = post.title;
    titleLabel.numberOfLines = 0;
    titleLabel.font = [UIFont systemFontOfSize:titleLabelFont];
    titleLabel.textColor = [UIColor lightGrayColor];
    NSDictionary *titileLabelAttri = @{NSFontAttributeName:[UIFont systemFontOfSize:titleLabelFont]};
    CGSize  titileLabelSize = [post.title boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 2*LmSpace, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:titileLabelAttri context:nil].size;
    titleLabel.frame = (CGRect){{LmSpace,LmSpace},titileLabelSize};
    [titleView addSubview:titleLabel];
    
    UILabel *timeLabel = [[UILabel alloc]init];
    timeLabel.textAlignment = NSTextAlignmentLeft;
    timeLabel.numberOfLines = 0;
    timeLabel.font = [UIFont systemFontOfSize:timeLabelFont];
    timeLabel.textColor = [UIColor lightGrayColor];
    NSDictionary *timeLabelAttri = @{NSFontAttributeName:[UIFont systemFontOfSize:timeLabelFont]};
    NSDateFormatter *fmt = [[NSDateFormatter alloc]init];
    fmt.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];
    fmt.dateFormat = @"MM月dd日' 'HH:mm";
    NSString *timeString = [fmt stringFromDate:post.createdAt];
    timeLabel.text = timeString;
    CGSize  timeLabelSize = [timeString boundingRectWithSize:CGSizeMake(SCREEN_WIDTH , MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:timeLabelAttri context:nil].size;
    timeLabel.frame = (CGRect){{0,0},timeLabelSize};
    timeLabel.y = CGRectGetMaxY(titleLabel.frame) +LmSpace;
    timeLabel.x = titleView.width - timeLabel.width - LmSpace;
    [titleView addSubview:timeLabel];
    
    UIView *titleViewBottomLine = [[UIView alloc]init];
    titleViewBottomLine.frame = CGRectMake(LmSpace, CGRectGetMaxY(timeLabel.frame) + LmSpace, titleView.width - 2 *LmSpace, 1);
    titleViewBottomLine.backgroundColor = RGBCOLOR(bottomLineRGBValue, bottomLineRGBValue, bottomLineRGBValue);
    [titleView addSubview:titleViewBottomLine];
    titleView.height = CGRectGetMaxY(titleViewBottomLine.frame);
    
    //正文栏
    _postBgView = [[UIImageView alloc]init];
    _postBgView.backgroundColor = [UIColor whiteColor];
    [self.bgView addSubview:_postBgView];
    _postBgView.frame = CGRectMake(0, CGRectGetMaxY(titleView.frame), SCREEN_WIDTH, 0);
    
    NSArray *contentArray = post.contentArray;
    NSDictionary *contentLabelAttri = @{NSFontAttributeName:[UIFont systemFontOfSize:contentLabelFont]};
    for (int index = 0; index < contentArray.count; index ++) {
        NSDictionary *dict = contentArray[index];
        NSString *content = dict[@"text"];
        NSString *keyString = [dict.allKeys firstObject];
        if ([keyString isEqualToString:@"text"]) {
            if (content.length) {
                UILabel *contentLabel = [[UILabel alloc]init];
                contentLabel.numberOfLines = 0;
                contentLabel.textColor = [UIColor lightGrayColor];
                contentLabel.font = [UIFont systemFontOfSize:contentLabelFont];
                NSString *content = dict[@"text"];
                contentLabel.text = content;
                CGSize  contentLabelSize = [content boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 2*LmSpace, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:contentLabelAttri context:nil].size;
                contentLabel.frame = (CGRect){{LmSpace,[self calculateY]},contentLabelSize};
                [_postBgView addSubview:contentLabel];
            }
        }else
        {
            NSURL *url = [NSURL URLWithString:dict[@"image"]];
            UIImageView *imageView = [[UIImageView alloc]init];
            [imageView yy_setImageWithURL:url placeholder:[UIImage imageNamed:@"占位图.png"]];
            NSString *scaleString = dict[@"scale"];
            double scale = [scaleString doubleValue];
            CGFloat imageViewX = 10;
            CGFloat imageViewY = [self calculateY];
            CGFloat imageViewW = SCREEN_WIDTH - 20;
            CGFloat imageViewH = imageViewW  / scale;
            imageView.frame = CGRectMake(imageViewX, imageViewY, imageViewW, imageViewH);
            [_postBgView addSubview:imageView];
        }
    }
    _postBgView.height = [self calculateY];
    self.bgView.contentSize = CGSizeMake(0, CGRectGetMaxY(_postBgView.frame));
}
#pragma mark - 从传入的post中加载

#pragma mark - 用取出的reply数据模型给控件赋值
- (void)setupReviewUIWithLMReply:(LMReply *)reply LMUser:(LMUser *)user
{
    //评论区
    LMReplyView *lastView = [self.replyBgView.subviews lastObject];
    CGFloat replyViewY = CGRectGetMaxY(lastView.frame);
    
    UIImageView *replyView = [[UIImageView alloc]init];
    replyView.backgroundColor = [UIColor whiteColor];
    replyView.frame = CGRectMake(0, replyViewY, SCREEN_WIDTH, 0);
    replyView.userInteractionEnabled = YES;
    UITapGestureRecognizer *replyTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(replyBgViewTap:)];
    [replyView addGestureRecognizer:replyTap];
    [_replyBgView addSubview:replyView];
    
    UIImageView *iconView = [[UIImageView alloc]init];
    iconView.frame = CGRectMake(LmSpace, LmSpace, 33, 33);
    [replyView addSubview:iconView];
    
    NSDictionary *nicknamelLabelAttri = @{NSFontAttributeName:[UIFont systemFontOfSize:nameLabelFont]};
    UILabel *nicknameLabel = [[UILabel alloc]init];
    nicknameLabel.textColor = [UIColor orangeColor];
    nicknameLabel.font = [UIFont systemFontOfSize:nameLabelFont];
    
    NSDictionary *replyContentAttri = @{NSFontAttributeName:[UIFont systemFontOfSize:contentLabelFont]};
    UILabel *replyContentLabel = [[UILabel alloc]init];
    replyContentLabel.textColor = [UIColor lightGrayColor];
    replyContentLabel.text = reply.replyText;
    replyContentLabel.numberOfLines = 0;
    replyContentLabel.font = [UIFont systemFontOfSize:contentLabelFont];
    CGSize replyContentLabelSize = [reply.replyText boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 2 *LmSpace, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:replyContentAttri context:nil].size;
    replyContentLabel.frame = (CGRect){{LmSpace,CGRectGetMaxY(iconView.frame) + LmSpace},replyContentLabelSize};
    [replyView addSubview:replyContentLabel];
    
    UIView *replyBottomLine = [[UIView alloc]init];
    replyBottomLine.frame = CGRectMake(LmSpace, CGRectGetMaxY(replyContentLabel.frame) + LmSpace, SCREEN_WIDTH - 2*LmSpace, 1);
    replyBottomLine.backgroundColor = RGBCOLOR(bottomLineRGBValue, bottomLineRGBValue, bottomLineRGBValue);
    [replyView addSubview:replyBottomLine];
    
    replyView.height = CGRectGetMaxY(replyBottomLine.frame);
    _replyBgView.height = CGRectGetMaxY(replyView.frame);
    self.bgView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.replyBgView.frame));
    [self.bgView.mj_header endRefreshing];
    
    [user fetchIfNeededInBackgroundWithBlock:^(AVObject *object, NSError *error) {
        [iconView yy_setImageWithURL:[NSURL URLWithString:user.iconImage.url] placeholder:nil];
        nicknameLabel.text = user.nickname;
        CGSize nicknameLabelSize = [user.nickname boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - iconView.width - 2 *LmSpace, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:nicknamelLabelAttri context:nil].size;
        nicknameLabel.frame = (CGRect){{CGRectGetMaxX(iconView.frame)+LmSpace,LmSpace},nicknameLabelSize};
        [replyView addSubview:nicknameLabel];
    }];
    
}

#pragma mark - 计算postBgView的Y位置
-  (CGFloat )calculateY;
{
    if (!self.postBgView.subviews.count == 0)
    {
        UIView *view = [self.postBgView.subviews lastObject];
        return  CGRectGetMaxY(view.frame) + 10;
    }else
    {
        return 10;
    }
    return 0;
}
#pragma mark - 跳转到更多评论
- (void)moreCommentBtnClick
{
    LMMoreCommentController *moreCommentVc = [LMMoreCommentController createWIthReplies:self.reviews andPost:self.post];
    [self.navigationController pushViewController:moreCommentVc animated:YES];
}
#pragma mark - 评论被点击
- (void)replyBgViewTap:(UITapGestureRecognizer *)tap
{
    
}
#pragma mark - 跳转到评论界面
- (void)toCommentVc
{
    LMCommentController *commentVc = [LMCommentController LMCommentControllerWIthPost:self.post];
    __weak typeof(self) weakSelf = self;
    [[commentVc rac_signalForSelector:@selector(commentComplete)] subscribeNext:^(id x) {
        [weakSelf refreshReviews];
    }];
    [self presentViewController:commentVc animated:YES completion:nil];
}

#pragma mark - 方便RAC代理方法 无实际作用
- (void)commentComplete
{
    LMMoreCommentController *moreCommentVc = [LMMoreCommentController createWIthReplies:nil andPost:nil];
    [self.navigationController pushViewController:moreCommentVc animated:YES];
}
#pragma mark - warning
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
#pragma mark - dealloc
- (void)dealloc
{
    NSLog(@"dealloc--profilePostVc");
}
@end
